package com.tegtech.tools.aliyun.support;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.tegtech.tools.aliyun.properties.AliyunProperties;

import lombok.extern.slf4j.Slf4j;

/**
 * 阿里短信服务类
 * @Author Jun
 * @date 2020年4月17日 下午5:24:49
 */
@Slf4j
public class SmsMessage {

	// 地域
	private static final String REGIONID = "cn-hangzhou";
	
	//产品名称
    private static final String PRODUCT = "Dysmsapi";
    
    //产品域名
    private static final String DOMAIN = "dysmsapi.aliyuncs.com";
	
	/**
	 * 短信发送
	 *
	 * @param phone 		多个手机号码用逗号隔开
	 * @param template 		短信模板
	 * @param templateParam 模板参数
	 */
	public static SendSmsResponse send(String phone, String template, String templateParam) {
        //初始化acsClient,暂不支持region化
        IClientProfile profile = DefaultProfile.getProfile(REGIONID, AliyunProperties.getAccessId(), AliyunProperties.getAccessSecret());
        SendSmsResponse sendSmsResponse = null;
        try {
			DefaultProfile.addEndpoint(REGIONID, PRODUCT, DOMAIN);
	        SendSmsRequest request = new SendSmsRequest();
	        request.setPhoneNumbers(phone);
	        request.setSignName(AliyunProperties.getSmsSignName());
	        request.setTemplateCode(template);
	        request.setTemplateParam(templateParam);
	        IAcsClient acsClient = new DefaultAcsClient(profile);
			sendSmsResponse = acsClient.getAcsResponse(request);
        }catch (Exception e) {
			log.error("阿里云短信发送失败：{}", sendSmsResponse.getMessage(), e);
		}
        return sendSmsResponse;
	}
}
